﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using FoursquareTask.Models;
using FoursquareTask.DAL;
using FoursquareTask.Managers;
using FoursquareTask.TOs;
using FoursquareTask.Exceptions;

namespace FoursquareTask.Controllers
{
    public class SearchController : Controller
    {
        //private FoursquareContext db = new FoursquareContext();

        private SearchManager manager = new SearchManager();

        public SearchManager Manager
        {
            get { return manager; }
            set { manager = value; }
        }    


        public ActionResult Index(Search search)
        {
            List<VenueTO> results = null;
            ViewBag.SearchedFor = search.Count;
            try 
            {
              results = Manager.GetTOs4Search(search);
            }
            catch(FoursquareException e)
            {
                return RedirectToAction("Unavail");
            }
            return View(results);  
        }

        public ActionResult Unavail()
        {
            return View();
        }


        // GET: /Search/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: /Search/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include="Text")] Search search)
        {
            if (ModelState.IsValid)
            {
                Search updatedSearch = Manager.NewSearch(search);
                return RedirectToAction("Index", updatedSearch);
            }

            return View(search);
        }        
    }
}
